package service;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.BlogDao;
import dao.FansDao;
import dao.UserDao;
import entry.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/author")
public class AuthorServlet extends HttpServlet {
    ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String authorId = req.getParameter("authorId");
        UserDao userDao = new UserDao();
        FansDao fansDao = new FansDao();
        resp.setContentType("application/json; charset=utf8");
        if (authorId != null){
            int id = Integer.parseInt(authorId);
            User user = userDao.getUserById(id);
            // 查询出用户的粉丝信息
            List<User> fansById = fansDao.getFansById(id);
            user.setFans(fansById);
            String retString = objectMapper.writeValueAsString(user);
            resp.getWriter().write(retString);
        }
    }
}
